//
//  YDWebViewController.m
//  biyingqiquan
//
//  Created by LK-IOS on 2018/5/3.
//  Copyright © 2018年 LK-BYQQIOS. All rights reserved.
//

#import "YDWebViewController.h"
#import "YDWebProgressLayer.h"
#import <WebKit/WebKit.h>

@interface YDWebViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic, strong)WKWebView *webview;
@property (nonatomic, strong) YDWebProgressLayer *webProgressLayer;  //  进度条

@end

@implementation YDWebViewController

- (instancetype)init
{
    if (self = [super init]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNav];
    [self.view addSubview:self.webview];
    
    DDLog(@"跳转网页的url==%@",self.urlStr);
    
    if (self.urlStr.length) {
        
        [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
    }
    
    
}



-(void)setNav
{
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    _webProgressLayer = [[YDWebProgressLayer alloc] init];
    _webProgressLayer.frame = CGRectMake(0, 42, LKScreenW, 2);
    
    [self.navigationController.navigationBar.layer addSublayer:_webProgressLayer];
}

#pragma mark - WKNavigationDelegate 页面加载的代理方法


- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    [_webProgressLayer startLoad];
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    if (!self.title.length) {
        [webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            
            self.title = response;
            
            
        }];
    }
    
    
    [_webProgressLayer finishedLoadWithError:nil];
}

-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    [_webProgressLayer finishedLoadWithError:error];
}


#pragma mark - H5弹窗要实现 要不不弹窗
-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }])];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - H5 tel标签拨打电话问题
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSURL *URL = navigationAction.request.URL;
    NSString *scheme = [URL scheme];
    UIApplication *app = [UIApplication sharedApplication];
    // 打电话
    if ([scheme isEqualToString:@"tel"]) {
        if ([app canOpenURL:URL]) {
            [app openURL:URL];
            // 一定要加上这句,否则会打开新页面
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
    }
    //    // 打开appstore
    //    if ([url.absoluteString containsString:@"ituns.apple.com"]) {
    //        if ([app canOpenURL:url]) {
    //            [app openURL:url];
    //            decisionHandler(WKNavigationActionPolicyCancel);
    //            return;
    //        }
    //    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

#pragma mark - getter
- (WKWebView *)webview
{
    if (!_webview) {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        WKPreferences *preferences = [WKPreferences new];
        preferences.javaScriptCanOpenWindowsAutomatically = YES;
        // preferences.minimumFontSize = 40.0;
        configuration.preferences = preferences;
        _webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
        
        _webview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _webview.UIDelegate = self;
        _webview.navigationDelegate = self;
        
        
    }
    return _webview;
}

@end
